import {TrayIcon} from '@tauri-apps/api/tray';
import {Menu} from '@tauri-apps/api/menu';

export default async function tray_init() {
    const menu = await Menu.new({
        items: [
            {
                id: 'info',
                text: '关于',
                action: () => {
                    console.log("info press")
                }
            },
            {
                id: 'quit',
                text: '退出',
                action: () => {
                    // 退出逻辑
                    const appWindow = new Window('main');
                    appWindow.close()
                }
            },
        ],
    });

    const options = {
        // 这里使用了绝对路径，要求这个目录下面必须存在该文件，否则系统托盘图标为透明，显然系统打包以后分发是无法做到这个图标也一起走的
        icon: "C:\Users\Mapleaf\Desktop\logo.png",
        menu,
        menuOnLeftClick: false,
        // 托盘行为
        action: (event) => {
            switch (event.type) {
                case 'Click':
                    console.log(
                        `mouse ${event.button} button pressed, state: ${event.buttonState}`
                    );
                    break;
                case 'DoubleClick':
                    // 在DoubleClick事件中添加了，双显示主窗口的逻辑，实现如下：
                    console.log(`mouse ${event.button} button pressed`);
                    // 双击图标显示主页面
                    const appWindow = new Window('main');
                    // 解除最小化
                    appWindow.unminimize();
                    // 显示主窗口
                    appWindow.show();
                    // 置于最顶层
                    appWindow.setFocus();
                    break;
                case 'Enter':
                    console.log(
                        `mouse hovered tray at ${event.rect.position.x}, ${event.rect.position.y}`
                    );
                    break;
                case 'Move':
                    console.log(
                        `mouse moved on tray at ${event.rect.position.x}, ${event.rect.position.y}`
                    );
                    break;
                case 'Leave':
                    console.log(
                        `mouse left tray at ${event.rect.position.x}, ${event.rect.position.y}`
                    );
                    break;
            }
        },
    };

    const tray = await TrayIcon.new(options);
}
